<?php

namespace app\index\controller;

use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Work\GroupRobot\Messages\Markdown;
use app\common\controller\Frontend;
use EasyWeChat\Factory;
use app\admin\model\Assistant as AssistantModel;
use EasyWeChat\Work\GroupRobot\Messages\NewsItem;
use EasyWeChat\Work\GroupRobot\Messages\Text;
use EasyWeChatComposer\EasyWeChat;
use fast\Http;

class Wxorder extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    protected $apps;
    protected $model;


    public function _initialize()
    {
        $this->apps = Factory::work(AssistantModel::config());
        $this->model = new \app\admin\model\Wxorder();
        parent::_initialize();
    }

    public function index()
    {
        if($this->request->isPost())
        {
            $data = $this->request->request();
            if (!isset($data['order_sn'])) return json(['msg'=>'请传入订单号！','code'=>2]);
            if (!isset($data['order_status']))  return json(['msg'=>'请传入订单状态！','code'=>2]);
            if(!$row = $this->model->get(['order_sn'=>$data['order_sn']]))  return json(['msg'=>'订单号不存在！','code'=>1]);
            //申请退货  发送通知 修改状态
            if($data['order_status'] == 10)
            {
                $row->state = $data['order_status'];
                // 通过构造函数传参
                $text = [
                    "chatid"=> "wrhInZCgAAURQ5YBWykfd2w1aDQ-xJ4g",
                    "msgtype"=>"text",
                    "text"=>["content" => "订单号：".$row->order_sn."\n状态：申请退款"],
                    "safe"=>0
                ];
            }
            //取消退货  发送通知 修改状态
            if($data['order_status'] == 9)
            {
                $row->state =1;
                // 通过构造函数传参
                $text = [
                    "chatid"=> "wrhInZCgAAURQ5YBWykfd2w1aDQ-xJ4g",
                    "msgtype"=>"text",
                    "text"=>["content" => "订单号：".$row->order_sn."\n状态：取消退货"],
                    "safe"=>0
                ];
            }
            //退货成功  发送通知 修改状态
            if($data['order_status'] == 11)
            {
                $row->state = $data['order_status'];
                // 通过构造函数传参
                $text = [
                    "chatid"=> "wrhInZCgAAURQ5YBWykfd2w1aDQ-xJ4g",
                    "msgtype"=>"text",
                    "text"=>["content" => "订单号：".$row->order_sn."\n状态：退款成功"],
                    "safe"=>0
                ];
            }
            //退货退款成功  发送通知 修改状态
            if($data['order_status'] == 12)
            {
                $row->state = $data['order_status'];
                // 通过构造函数传参
                $text = [
                    "chatid"=> "wrhInZCgAAURQ5YBWykfd2w1aDQ-xJ4g",
                    "msgtype"=>"text",
                    "text"=>["content" => "订单号：".$row->order_sn."\n状态：退货、退款成功"],
                    "safe"=>0
                ];
            }
            $this->apps->chat->send($text);
            $row->save();
            return json(['msg'=>'操作成功！','code'=>0]);
        }
        return json(['msg'=>'失败！','code'=>2]);
    }
}
